﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace B2BToolKitControl
{
    public class B2BCommandProvider
    {
        public enum Cmd_Control : byte
        {
            Cmd_Set_DSG_Load_Gate = (0x70),
            Cmd_Set_CHG_Shiftet_Gate = (0x71),
            Cmd_Set_ADC_VPC_Gate = (0x72),
            Cmd_Set_ADC_VPD_Gate = (0x73),

            Cmd_Communi_Multiplex_Reset = (0x80),
            Cmd_Communi_Multiplex_Set_Channel = (0x81),
            Cmd_UART_Set_Baud_Rate = (0x82),
            Cmd_UART_Set_Default_Baud_Rate = (0x83),
            Cmd_UART_RS485_Enable = (0x84),
            Cmd_UART_RS485_Disable = (0x85),
            Cmd_One_Wire_Commu_Enable = (0x86),
            Cmd_One_Wire_Commu_Disable = (0x87),
            Cmd_I2C_Reset = (0x88),  //no used on Ver 2.0
            Cmd_I2C_Set_Address = (0x89),  //no used on Ver 2.0

            //Charger Cmd
            Cmd_Charger_24V_Channel_Set_ID = (0x8A),  //switch Mux, set input gate and set ID Levels
            Cmd_Charger_36V_Channel_Set_ID = (0x8B),  //switch Mux, set input gate and set ID Levels
            Cmd_Charger_48V_Channel_Set_ID = (0x8C),  //switch Mux, set input gate and set ID Levels
            Cmd_Get_Charger_24V_Voltage_Auto = (0x8D),  //auto switching three levels
            Cmd_Get_Charger_36V_Voltage_Auto = (0x8E),  //auto switching three levels
            Cmd_Get_Charger_48V_Voltage_Auto = (0x8F),  //auto switching three levels
            Cmd_Charger_24V_Channel_Set_Vin = (0xA0),  //switch Mux, set input gate but set ID Levels Off
            Cmd_Charger_36V_Channel_Set_Vin = (0xA1),  //switch Mux, set input gate but set ID Levels Off
            Cmd_Charger_48V_Channel_Set_Vin = (0xA2),  //switch Mux, set input gate but set ID Levels Off
            Cmd_Charger_All_Channel_ID_Set_OFF = (0xA3),  //switch Mux empty, set input gate off and set ID Levels Off
            Cmd_Charger_All_Channel_Set_Vin = (0xA4),  //orginal function for version1, no Used on Ver 2.0
            Cmd_Get_All_Charger_Channel_Voltage = (0xA5),  //no Used on Ver 2.0
            Cmd_Auto_Charger_Checking = (0xA6),  //no Used on Ver 2.0
            Cmd_Fast_Auto_Charger_Checking = (0xA7),  //no Used on Ver 2.0
            Cmd_Get_PACK_DSG_Voltage_Auto = (0xA8),  //auto turn on gate
            Cmd_Get_PACK_CHG_Voltage_Auto = (0xA9),  //auto turn on gate
            Cmd_Get_Channel_Raw_ADC = (0xAA),
            Cmd_Get_Direct_PackDSG_Voltage = (0xAB),
            Cmd_Get_Direct_PackCHG_Voltage = (0xAC),
            Cmd_Get_Direct_Chger_24Voltage = (0xAD),
            Cmd_Get_Direct_Chger_36Voltage = (0xAE),
            Cmd_Get_Direct_Chger_48Voltage = (0xAF),
            Cmd_Get_Direct_DSG_Current = (0xB0),
            Cmd_Get_Direct_CHG_Current = (0xB1),
            Cmd_Get_Charger_Is_ID_Level = (0xB2)
        }

        public enum Cmd_DataTransmit : byte
        {

            // Data Status cmd
            Cmd_I2C_Transmit_Data = (0x90),
            Cmd_I2C_Receive_Data = (0x91),
            Cmd_UART_Transmit_Data = (0x92),
            Cmd_UART_Receive_Data = (0x93),
            Cmd_One_Wire_Transmit_Data = (0x94),
            Cmd_One_Wire_Receive_Data = (0x95),
        }
        public enum Cmd_Calibration : byte
        {
            // Calibration Status cmd                           
            Cmd_Cal_Set_Charger_24V_Channel_Offset = (0xD0),
            Cmd_Cal_Set_Charger_36V_Channel_Offset = (0xD1),
            Cmd_Cal_Set_Charger_48V_Channel_Offset = (0xD2),
            Cmd_Get_All_Calibration_Data = (0xD3),
            Cmd_Get_All_Flash_Data = (0xD4),
            Cmd_Set_Cal_Data_To_Flash = (0xD5),
            Cmd_Cal_Set_PACK_DSG_Vol_CAL_ADC_offset = (0xD6),
            Cmd_Cal_Set_PACK_CHG_Vol_CAL_ADC_offset = (0xD7),
            Cmd_Cal_Set_DSG_Current_CAL_ADC_offset = (0xD8),
            Cmd_Cal_Set_CHG_Current_CAL_ADC_offset = (0xD9),

            Cmd_Error_Cmd = (0xE0),
            Cmd_For_Connect_Detection = (0xE1),
            Cmd_Test_Data_Send_Back = (0xE2),
            Cmd_Test_Get_All_Raw_ADC_Data = (0xE3),

            Cmd_FW_HW_Version = (0xE5),
            Cmd_SetDetectCharger_DelayCycle = (0xE6),
        }

        public enum RespondCode : byte
        {
            Respond_Error_Check_Code = (0xF3),
            Respond_Accept_Check_Code = (0xF0)
        }

        public static byte[] CmdToB2BToolKit(byte ToolKitCmd, byte[] parameters)
        {
            return LEVUtilityLibrary.B2BToolKit_USBPacketFormat.Packing(ToolKitCmd, parameters);
        }
        public static byte[] CmdToB2BToolKit<T>(T ToolKitCmd, byte[] parameters) where T : struct, IConvertible
        {

            bool flag = false;
            //string str = "";
            if (!typeof(T).IsEnum) throw new ArgumentException("ToolKitCmd Error, T must be an enumerated type of ToolKit Cmd");
            if (typeof(Cmd_Control) == typeof(T) ||
                typeof(Cmd_DataTransmit) == typeof(T) ||
                typeof(Cmd_Calibration) == typeof(T))
            {
                flag = true;
            }
            if (!flag) throw new ArgumentException("ToolKitCmd Error, T must be an enumerated type of ToolKit Cmd");
            byte cmd;
            //Cmd_UART_RS485_Transmit_Data = (0x92),
            //str = Enum.GetName(typeof(T), ToolKitCmd); //= Cmd_UART_RS485_Transmit_Data
            //str = ((byte)Enum.Parse(typeof(T), ToolKitCmd.ToString())).ToString(); // = 0x92
            //str = typeof(T).ToString(); // = B2BToolKitControl.Cmd_ToolKitDataTransmit
            //str = ToolKitCmd.GetType().ToString(); // = B2BToolKitControl.Cmd_ToolKitDataTransmit
            cmd = (byte)Enum.Parse(typeof(T), ToolKitCmd.ToString());
            byte[] sendingCmd = LEVUtilityLibrary.B2BToolKit_USBPacketFormat.Packing(cmd, parameters);
            //str = ToolKitCmd.GetType().ToolKitCmd;
            return sendingCmd;
            //{
            //}
            //LEVUtilityLibrary.B2BToolKit_USBPacketFormat.Packing(Enum. 
        }
        public static byte[] CmdToLevBatteryViaB2BToolKit(byte BatteryCmd, byte[] parameters)
        {
            byte[] temp = LEVBatteryPackControl.LEVBPCommandProvider.CmdToLEVBP(BatteryCmd, parameters);
            return CmdToB2BToolKit<Cmd_DataTransmit>(Cmd_DataTransmit.Cmd_UART_Transmit_Data, temp);
        }
        public static byte[] CmdToLevBatteryViaB2BToolKit<T>(T BatteryCmd, byte[] parameters) where T : struct, IConvertible
        {
            byte[] temp = LEVBatteryPackControl.LEVBPCommandProvider.CmdToLEVBP<T>(BatteryCmd, parameters);
            return CmdToB2BToolKit<Cmd_DataTransmit>(Cmd_DataTransmit.Cmd_UART_Transmit_Data, temp);
        }
    }//public class B2BCommandProvider

}//namespace B2BToolKitControl
